﻿<%@ WebHandler Language="C#" Class="TopicHandler" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;

public class TopicHandler : IHttpHandler,IRequiresSessionState {
    
    public void ProcessRequest (HttpContext context) {
        if (context.Request.Params["act"] != null)
        {
            switch (context.Request.Params["act"].ToLower())
            {
                case "get":
                    Get(context);
                    break; 
                case "getcount":
                    GetCount(context);
                    break;
                case "gettopic":
                    GetTopic(context);
                    break;
                case "getreply":
                    GetReply(context);
                    break;
                case "addreply":
                    AddReply(context);
                    break;
            }
        }
    }

    public void Get(HttpContext context)
    {
        System.Data.DataSet ds = new System.Data.DataSet();
        int num;
        int page=1;
        if (context.Request["page"] != null)
        {
            int p = Convert.ToInt32(context.Request["page"]);
            if (p > 1)
                page = p; 
        }
        ds = new Cstudy.BLL.Topics().SeletTopic(page, 10, out num);
        IList<Cstudy.MODEL.Topics> topics = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.Topics>(ds.Tables[0]);
        string json = Cstudy.Tools.JsonHelper.ToJson(topics);
        json = "{\"Data\":" + json + ",\"count\":\"" + num.ToString() + "\"}";
        context.Response.ContentType = "json/application";
        context.Response.Write(json);
        context.Response.End();
    }

    public void GetCount(HttpContext context)
    {
        System.Data.DataSet ds = new System.Data.DataSet();
        int num;
        int page = 1;
        if (context.Request["page"] != null)
        {
            int p = Convert.ToInt32(context.Request["page"]);
            if (p > 1)
                page = p;
        }
        ds = new Cstudy.BLL.Topics().SeletTopic(page, 10, out num);
        IList<Cstudy.MODEL.Topics> topics = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.Topics>(ds.Tables[0]);
        string json = "{\"count\":\"" + num.ToString() + "\"}";
        context.Response.ContentType = "json/application";
        context.Response.Write(json);
        context.Response.End();
    }

    public void GetTopic(HttpContext context)
    {
        System.Data.DataSet ds = new System.Data.DataSet();
        if (context.Request["tid"] != null)
        {
            ds = new Cstudy.BLL.Topics().SelectTopicByID(Convert.ToInt32(context.Request["tid"]));
            Cstudy.MODEL.Topics topic = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.Topics>(ds.Tables[0].Rows[0]);
            string json = Cstudy.Tools.JsonHelper.ToJson(topic);
            context.Response.ContentType = "json/application";
            context.Response.Write(json);
            context.Response.End();
        }
    }

    public void GetReply(HttpContext context)
    {
        if (context.Request["tid"] != null)
        {
            System.Data.DataSet ds = new System.Data.DataSet();
            ds = new Cstudy.BLL.VReply().SelectReply(Convert.ToInt32(context.Request["tid"]));
            if (ds != null)
            {
                IList<Cstudy.MODEL.VReply> replys = Cstudy.Tools.LoadModel.Deserialize<Cstudy.MODEL.VReply>(ds.Tables[0]);
                string json = Cstudy.Tools.JsonHelper.ToJson(replys);
                context.Response.ContentType = "json/application";
                context.Response.Write(json);
                context.Response.End();
            } 
        } 
    }

    public void AddReply(HttpContext context)
    {
        if (context.Request["tid"] != null)
        {
            string json;
            Cstudy.MODEL.reply reply = new Cstudy.MODEL.reply();
            Cstudy.MODEL.users user = GetLoginStates(context);
            if (user == null)
            {
                json = "{\"success\":\"false\", \"result\":\"未登陆\"}";
                context.Response.ContentType = "json/application";
                context.Response.Write(json);
                context.Response.End();
                return;
            }
            reply.AuthorID = user.UserID;
            reply.TopicID = Convert.ToInt32(context.Request["tid"]);
            reply.Content = context.Request["content"];
            if (new Cstudy.BLL.reply().AddReply(reply))
            {
                json = "{\"success\":\"true\",\"result\":\"" + user.UserName + "\"}";
            }
            else
            {
                json = "{\"success\":\"false\",\"result\":\"插入数据失败\"}";
            }
            context.Response.ContentType = "json/application";
            context.Response.Write(json);
            context.Response.End();
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

    protected Cstudy.MODEL.users GetLoginStates(HttpContext context)
    {
        if (context.Request.Cookies["user"] != null)
        {
            Cstudy.MODEL.users user = new Cstudy.MODEL.users();
            HttpCookie cookies = context.Request.Cookies["user"];
            user.UserID = int.Parse(cookies.Values["userid"]);
            user.UserName = cookies.Values["name"];
            return user;
        }
        return null;
    }
}